﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Jack.DataScience.Data.Converters
{
    public static class DateTimeExtension
    {
        public static DateTime DBNull = Convert.ToDateTime("1900-01-01 00:00:00");

        /// <summary>
        ///     判断DateTime在数据库中是否存储为象征意义的null
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static bool IsDBNull(this DateTime dateTime)
        {
            return dateTime == DBNull;
        }

        /// <summary>
        ///     判断DateTime在数据库中是否存储为象征意义的null
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static bool IsDBNull(this DateTime? dateTime)
        {
            return dateTime == null || dateTime == DBNull;
        }

        /// <summary>
        ///     格式化为字符串
        /// </summary>
        /// <param name="dateTime"></param>
        /// <returns></returns>
        public static string Format(this DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
        }

        // todo: 日期渲染成什么格式 是前端需要处理 不应该放在后端
        /// <summary>
        /// 返回当前日期的星期名称
        /// </summary>
        /// <param name="dt">日期</param>
        /// <returns>星期名称</returns>
        public static string GetWeekNameOfDay(DateTime dateTime)
        {
            switch (dateTime.DayOfWeek)
            {
                case DayOfWeek.Monday:
                    return "星期一";
                case DayOfWeek.Tuesday:
                    return "星期二";
                case DayOfWeek.Wednesday:
                    return "星期三";
                case DayOfWeek.Thursday:
                    return "星期四";
                case DayOfWeek.Friday:
                    return "星期五";
                case DayOfWeek.Saturday:
                    return "星期六";
                case DayOfWeek.Sunday:
                    return "星期日";
            }
            return null;
        }

        /// <summary>
        /// 返回两个日期之间相差的时间数
        /// </summary>
        /// <param name="howToCompare">比较什么</param>
        /// <param name="startDate">开始时间</param>
        /// <param name="endDate">结束时间</param>
        /// <returns></returns>
        public static int DateDiff(string howToCompare, DateTime startDate, DateTime endDate)
        {
            int diff = 0;

            TimeSpan TS = new TimeSpan(endDate.Ticks - startDate.Ticks);

            switch (howToCompare.ToLower())
            {
                case "year":
                    diff = Convert.ToInt32(TS.TotalDays / 365);
                    break;
                case "month":
                    diff = Convert.ToInt32((TS.TotalDays / 365) * 12);
                    break;
                case "day":
                    diff = Convert.ToInt32(TS.TotalDays);
                    break;
                case "hour":
                    diff = Convert.ToInt32(TS.TotalHours);
                    break;
                case "minute":
                    diff = Convert.ToInt32(TS.TotalMinutes);
                    break;
                case "second":
                    diff = Convert.ToInt32(TS.TotalSeconds);
                    break;
            }

            return diff;
        }
        public static int GetAge(this DateTime birthday)
        {
            DateTime now = DateTime.Now;
            int age = now.Year - birthday.Year;
            if (now.Month < birthday.Month || (now.Month == birthday.Month && now.Day < birthday.Day))
            {
                age--;
            }
            return age < 0 ? 0 : age;
        }
        // todo: 两个datetime是可以直接相减变成TimeSpan然后计算时间差的
        public static string GetAge(string birthday, DateTime dtNow)
        {
            try
            {

                string strAge = string.Empty; // 年龄的字符串表示
                int intYear = 0; // 岁
                int intMonth = 0; // 月
                int intDay = 0; // 天

                // 如果没有设定出生日期, 返回空
                if (string.IsNullOrWhiteSpace(birthday))
                {
                    return string.Empty;
                }
                var dtBirthday = birthday.TryDateTime();
                // 计算天数
                intDay = dtNow.Day - dtBirthday.Day;
                if (intDay < 0)
                {
                    dtNow = dtNow.AddMonths(-1);
                    intDay += DateTime.DaysInMonth(dtNow.Year, dtNow.Month);
                }

                // 计算月数
                intMonth = dtNow.Month - dtBirthday.Month;
                if (intMonth < 0)
                {
                    intMonth += 12;
                    dtNow = dtNow.AddYears(-1);
                }

                // 计算年数
                intYear = dtNow.Year - dtBirthday.Year;

                // 格式化年龄输出
                if (intYear >= 1) // 年份输出
                {
                    strAge = intYear.ToString() + "岁";
                }

                if (intMonth > 0 && intYear <= 5) // 五岁以下可以输出月数
                {
                    strAge += intMonth.ToString() + "个月";
                }

                if (intDay >= 0 && intYear < 1) // 一岁以下可以输出天数
                {
                    if (strAge.Length == 0 || intDay > 0)
                    {
                        strAge += intDay.ToString() + "天";
                    }
                }

                return strAge;
            }
            catch (Exception ex)
            {
                return ex.ToString();
            }
        }
        /// <summary>
        /// 取间隔月龄
        /// </summary>
        /// <param name="age"></param>
        /// <param name="round"></param>
        /// <returns></returns>
        public static int GetAgeSplit(decimal age, decimal round)
        {
            if (age >= 12)
            {
                var num = age / round;
                if (num >= 12)
                    return GetAgeSplit(age, round + 1);
                else
                {
                    return (int)round;
                }
            }
            else
            {
                return 1;
            }

        }

        //上周一日期

        public static DateTime GetLastMondayDate()
        {
            DateTime dt = DateTime.Now;
            int today = (int)dt.DayOfWeek;
            if (dt.DayOfWeek.ToString() != "Sunday")//也可以使用today!=0
            {
                return dt.AddDays(-today - 6).Date;
            }
            else
            {
                return dt.AddDays(-today - 13).Date;//若今天是周日，获取到的上周一的日期是本周周一的日期，所以要减去7天
            }
        }

        //上周日日期
        public static DateTime GetLastSundayDate()
        {
            DateTime dt = DateTime.Now;
            int today = (int)dt.DayOfWeek;
            if (dt.DayOfWeek.ToString() != "Sunday")//也可以使用today!=0
            {
                return dt.AddDays(-today).Date;
            }
            else
            {
                return dt.AddDays(-today - 7).Date;//若今天是周日，获取到的上周日的日期是本周周日的日期，所以要减去7天
            }
        }

    }
}
